import request from '@/utils/request'
/* 文件上传接口封装 file.js */
export function uploadFileAPI(file) {
  let formData = new FormData();
  formData.append("singleFile", file);  
  return request.post("/upload/single", formData, {
  	headers: {
  		'content-type': 'multipart/form-data' //
  	},  	
  });
}


/**七牛云文件上传API封装*/
//七牛云:获取七牛上传凭证upToken(通过后端接口)
export function qiniuGetUpToken() {
  return request({
    url: '/upload/qiniuUpToken',
    method: 'get'
  });
}

//七牛云:以表单方式直传 (没有通过后端接口)
//参考:https://developer.qiniu.com/kodo/manual/1272/form-upload
export async function  qiniuUpload(formData,key) {
  //首先获取七牛上传凭证upToken(通过后端接口)
  let result = await qiniuGetUpToken();
  if(result.code != 1) return Promise.reject(new Error(result.msg || 'Error'));
  let upToken = result.data;

  //以表单方式直接上传文件到七牛云空间（华东-浙江2)
  formData.append('key',key);
  formData.append('token', upToken);
  return request({
    url: 'http://up-cn-east-2.qiniup.com',
    baseURL: '',
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

//七牛云:获取上传的文件外链(通过后端接口)
export function qiniuGetPrivateDownloadUrl(key){
  return request({
    url: '/upload/qiniuPrivateDownloadUrl',
    method: 'get',
    params: {key},
  });
}
